home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cpptut22.zip / CHAP05.TXT < prev    next >
Text File  |  1992-01-20  |  45KB  |  944 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 5
  8.                                                     ENCAPSULATION
  9.  
  10. As mentioned in Chapter 1, object oriented programming will seem
  11. very unnatural to a programmer with a lot of procedural programming
  12. experience.  This chapter is the beginning of the definition of
  13. object oriented programming, and we will study the topic of
  14. encapsulation which is a "divide and conquer" technique.  As we
  15. stated earlier, there are a lot of new terms used with object
  16. oriented programming.  Don't be intimidated by the new terminology,
  17. we will study the terms one at a time in a meaningful order.
  18.  
  19. Encapsulation is the process of forming objects which we will
  20. discuss throughout this chapter.  An encapsulated object is often
  21. called an abstract data type and it is what object oriented
  22. programming is all about.  Without encapsulation, which involves
  23. the use of one or more classes, there is no object oriented
  24. programming.  Of course there are other topics concerning object
  25. oriented programming, but this is the cornerstone.
  26.  
  27.  
  28. WHY BOTHER WITH ENCAPSULATION?
  29. _________________________________________________________________
  30.  
  31. We need encapsulation because we are human, and humans make errors.
  32. When we properly encapsulate some code, we actually build an
  33. impenetrable wall to protect the contained code from accidental
  34. corruption due to the silly little errors that we are all prone to
  35. make.  We also tend to isolate errors to small sections of code to
  36. make them easier to find and fix.  We will have a lot more to say
  37. about the benefits of encapsulation as we progress through the
  38. tutorial.
  39.  
  40.  
  41. NO INFORMATION HIDING
  42. _________________________________________________________________
  43.  
  44. The program named OPEN.CPP is a really stupid      ==============
  45. program because it does next to nothing, but it       OPEN.CPP
  46. will be the beginning point for our discussion     ==============
  47. of encapsulation, otherwise known as information
  48. hiding.  Information hiding is an important part
  49. of object oriented programming and you should have a good grasp of
  50. what it is by the time we finish this chapter.
  51.  
  52. A very simple structure is defined in lines 4 through 6 which
  53. contains a single int type variable within the structure.  This is
  54. sort of a silly thing to do but it will illustrate the problem we
  55. wish to overcome in this chapter.  Three variables are declared in
  56. line 10, each of which contains a single int type variable and each
  57.  
  58.                                                          Page 5-1
  59.  
  60.                                         Chapter 5 - Encapsulation
  61.  
  62. of the three variables are available anywhere within the main
  63. function.  Each variable can be assigned, incremented, read,
  64. modified, or have any number of operations performed on it.  A few
  65. of the operations are illustrated in lines 13 through 21 and should
  66. be self explanatory to anyone with a little experience with the C
  67. programming language.
  68.  
  69. An isolated local variable named piggy is declared and used in the
  70. same section of code to illustrate that there is nothing magic
  71. about this code.
  72.  
  73. Study this simple program carefully because it is the basis for
  74. beginning our study of encapsulation.  Be sure to compile and
  75. execute this program, then we will go on to the next example
  76. program.
  77.  
  78.  
  79. INFORMATION HIDING
  80. _________________________________________________________________
  81.  
  82. Examine the program named CLAS.CPP for our first   ==============
  83. example of a program with a little information        CLAS.CPP
  84. hiding contained in it.  This program is           ==============
  85. identical to the last one except for the way it
  86. does a few of its operations.  We will take the
  87. differences one at a time and explain what is happening here.  Keep
  88. in mind that this is a trivial program and the safeguards built
  89. into it are not needed for such a simple program but are used here
  90. to illustrate how to use these techniques in a larger much more
  91. complicated program.
  92.  
  93. The first difference is that we have a class instead of a structure
  94. beginning in line 4 of this program.  The only difference between
  95. a class and a structure is that a class begins with a private
  96. section whereas a structure has no private section automatically
  97. defined.  The keyword class is used to declare a class as
  98. illustrated here.
  99.  
  100. The class named one_datum is composed of the single variable named
  101. data_store and two functions, one named set() and the other named
  102. get_value().  A more complete definition of a class is a group of
  103. variables and one or more functions that can operate on that data.
  104. Stay with us, we will tie this all together in a meaningful and
  105. useful way very soon.
  106.  
  107.  
  108. WHAT IS A PRIVATE SECTION?
  109. _________________________________________________________________
  110.  
  111. A private section of a class is a section of data which cannot be
  112. accessed outside of the class, it is hidden from any outside
  113. access.  Thus, the variable named data_store which is a part of the
  114. object (an object will be defined completely later) named dog1
  115. declared in line 23 is not available for use anywhere in the main
  116.  
  117.                                                          Page 5-2
  118.  
  119.                                         Chapter 5 - Encapsulation
  120.  
  121. program.  It is as if we have built a "brick wall" around the
  122. variables to protect them from accidental corruption by outside
  123. programming influences.  It seems a little dumb to declare a
  124. variable in the main program that we cannot use, but that is
  125. exactly what we did.
  126.  
  127. Figure 5-1 is a graphical representation of the class with its
  128. "brick wall" built around the data to protect it.  You will notice
  129. the small peep holes we have opened up to allow the user to gain
  130. access to the functions.  The peep holes were opened by declaring
  131. the functions in the public section of the class.
  132.  
  133.  
  134. WHAT IS A PUBLIC SECTION?
  135. _________________________________________________________________
  136.  
  137. A new keyword, public, is introduced in line 6 which states that
  138. anything following this keyword can be accessed from outside of
  139. this class.  Because the two functions are defined following the
  140. keyword public, they are both public and available for use in the
  141. calling function or any other function that is within the scope of
  142. the calling function.  This opens two small peepholes in the solid
  143. wall of protection.  You should keep in mind that the private
  144. variable is not available to the calling program.  Thus, we can
  145. only use the variable by calling one of the two functions defined
  146. as a part of the class.  These are called member functions because
  147. they are members of the class.
  148.  
  149. Since we have declared two functions, we need to define them by
  150. saying what each function will actually do.  This is done in lines
  151. 11 through 19 where they are each defined in the normal way, except
  152. that the class name is prepended onto the function name and
  153. separated from it by a double colon.  These two function
  154. definitions are called the implementation of the functions.  The
  155. class name is required because we can use the same function name
  156. in other classes and the compiler must know with which class to
  157. associate each function implementation.
  158.  
  159. One of the key points to be made here is that the private data
  160. contained within the class is available within the implementation
  161. of the member functions of the class for modification or reading
  162. in the normal manner.  You can do anything with the private data
  163. within the function implementations which are a part of that class,
  164. but the private data of other classes is hidden and not available
  165. within the member functions of this class.  This is the reason we
  166. must prepend the class name to the function names of this class
  167. when defining them.
  168.  
  169. It would be well to mention at this point that it is legal to
  170. include variables and functions in the private part and additional
  171. variables and functions in the public part.  In most practical
  172. situations, variables are included in only the private part and
  173. functions are included in only the public part of a class
  174. definition.  Occasionally, variables